package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.Message;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class MessageDao {
	
	DbHelp<Message> db = new DbHelp<Message>();
	
	public int save(Message mess){
		String sql = "insert into t_message(content,createtime,t_user_id,t_project_id)values(?,?,?,?)";
		return db.executeUpdateSql(sql, mess.getContent(),mess.getCreateTime(),mess.getUserId(),mess.getProjId());
		
	}
	
	public List<Message>selectForAll(){
		String sql = "select id,content,createtime,t_user_id,t_project_id from t_message";
		return db.exeSelectForList(sql, new PackgeMessage());
		
	}
	
	private class PackgeMessage implements ResultSetPackge<Message>{

		public Message packgeResultSet(ResultSet rs) throws SQLException {
				Message mess = new Message();
				mess.setId(rs.getInt("id"));
				mess.setCreateTime(rs.getString("createtime"));
				mess.setContent(rs.getString("content"));
				mess.setProjId(rs.getInt("t_project_id"));
				mess.setUserId(rs.getInt("t_user_id"));
				return mess;
		}
		
	}

	public List<Message> selectAllByProjId(int projectId) {
		String sql = "select id,content,createtime,t_user_id,t_project_id from t_message where t_project_id = ?";
		return db.exeSelectForList(sql, new PackgeMessage(),projectId);
	}

	public List<Message> selectAllByProjectId(int projectId) {
		String sql = "select id,content,createtime,t_user_id,t_project_id from t_message where t_project_id = ?";
		return db.exeSelectForList(sql, new PackgeMessage(),projectId);
	}

}
